<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 栏目分类控制器
 * @author wwj
 * @version 1.0.1 2012-12-11
 */
class Column extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		
		// 后台登录验证
		$this->check_login();
		
		// 验证权限
		$this->verify_purview(PURVIEW_COLUMN);
		
		$this->load->model('column_m');

		// 加载友情链接的语言包
		$this->load->language('column');
		
		$this->load->helper('column');
	}
	
	/**
	 * 栏目分类管理默认方法，指向栏目列表页
	 */
	function index()
	{
		$this->column_manage();
		
	}
	
	/**
	 * 栏目分类列表管理
	 */
	function column_manage()
	{
		$class 	= array(); 
		
		// 获取栏目分类的数组
		$class = $this->get_column_as_array();
		$data['class']			= $class;
		$data['header'] 		= $this->header;
		$data['title'] 			= $this->lang->line('column_page_label');
		$data['page_label']		= $this->lang->line('column_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$this->load->view('column_list', $data);
	}
	
	/**
	 * 添加新栏目分类
	 */
	function add_action()
	{
		// 对表单数据做判断
		$data = array(
				array('field' => 'columnname',  'label' => 'lang:column_name', 'rules' => 'trim|required|max_length[20]|xss_clean'),
				array('field' => 'fatherid',  'label' => 'lang:column_belong_to', 'rules' => 'trim|required|is_natural'),
				array('field' => 'isrecommend',  'label' => 'lang:column_recommend', 'rules' => 'trim|required|is_natural')
				);
		$validate = $this->validation($data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('column/column_manage'), $validate, 'goback');
		}
		$column = $this->input->post();
		if ($column && isset($column['s_column_add']))
		{
			unset($column['s_column_add']);
			$result = $this->column_m->insert_column($column);
			if ($result != FALSE)
			{
				$this->_show_message(base_url('column/column_manage'), $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('column/column_manage'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('column/column_manage'), $this->lang->line('error_invalid_post'));
		}
	}
	
	/**
	 * ajax方式删除栏目分类，根据指定id，或id数组删除栏目分类
	 */
	function delete_action()
	{
		// 对表单数据做判断
		$validate = $this->validation(array(array('field' => 'id[]', 'label' => 'lang:column_id', 'rules' => 'trim|required|is_natural|xss_clean')));
		if (TRUE !== $validate)
		{
			echo $this->array2json(TRUE, $validate);
			return;
		}
		
		// 获取栏目ID值，并放入数组
		$column_id['id'] = $this->input->post('id');
		
		// @TODO 判断栏目要删除的栏目中是否有文章，有不可删除
		// @TODO 判断栏目下是否还有子类，有子类，不能删除，将要删除的栏目id去掉
		foreach ($column_id['id'] as $k => $id)
		{
			$sub_class = $this->column_m->get_column(array('fatherid' => $id));
			if (empty($sub_class))
			{
				unset($column_id['id'][$k]);
			}
		}
		if ($this->column_m->delete_column($column_id))
		{
			echo $this->array2json(FALSE, $this->lang->line('msg_operation_success'));
			return ;
		}
		else
		{
			echo $this->array2json(TRUE, $this->lang->line('msg_operation_failed'));
			return ;
		}
	}
	
	/**
	 * 显示编辑栏目分类页面
	 * @param int $id 栏目分类id
	 */
	function edit($id)
	{
		$column_id['id'] = intval($id);
		if (empty($column_id))
		{
			$this->_show_message(base_url('column'), $this->lang->line('column_id_unable_empty'), 'goback');
		}
		
		// 查询
		$column = $this->column_m->get_column($column_id);
		if (empty($column))
		{
			$this->_show_message(base_url('column'), $this->lang->line('column_does_not_exist'), 'goback');
		}
		
		$data = array (
			
				'header' => $this->header,
				'column' => $column[0],
				'class' => $this->get_column_as_array(0, $column_id ['id']),
				'title' => $this->lang->line('column_title_edit'),
				'page_label' => $this->lang->line('column_page_label'),
				'nav_label' => $this->lang->line('com_navigate_label') 
		);
		$this->load->view('column_edit', $data);
	}
	
	/**
	 * 编辑栏目信息
	 */
	function edit_action()
	{
		// 对表单数据做判断
		$data = array(
				array('field' => 'columnname',  'label' => 'lang:column_name', 'rules' => 'trim|required|max_length[20]|xss_clean'),
				array('field' => 'fatherid',  'label' => 'lang:column_belong_to', 'rules' => 'trim|required|is_natural'),
				array('field' => 'isrecommend',  'label' => 'lang:column_recommend', 'rules' => 'trim|required|is_natural'),
				array('field' => 'id', 'label' => 'lang:column_id', 'rules' => 'trim|required|is_natural')
		);
		$validate = $this->validation($data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('column/column_manage'), $validate, 'goback');
		}
		$column = $this->input->post();
		
		// @TODO 判断要更新的id的父类是否存在，是否是从根结点开始的。
		if ($column && isset($column['s_column_edit']))
		{
			$column_id['id'] = intval($column['id']);
			unset($column['s_column_edit']);
			unset($column['id']);
			$result = $this->column_m->update_column($column_id, $column);
			$this->_show_message(base_url('column/column_manage'), $this->lang->line('msg_operation_success'));
		}
	}
}

/* End of file column.php */
/* Location: ./application/controllers/column.php */